package com.cays.myresearch.activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.cays.myresearch.R;
import com.cays.myresearch.Util.HttpUtil;
import com.cays.myresearch.action.LoginAction;
import com.cays.myresearch.constant.Constant;
import com.cays.myresearch.entity.Classes;
import com.cays.myresearch.entity.ClassesPay;
import com.cays.myresearch.entity.ResponseInfo;
import com.cays.myresearch.entity.Staffer;
import com.cays.myresearch.entity.Student;
import com.cays.myresearch.entity.Teacher;
import com.cays.myresearch.entity.UserInfo;
import com.cays.myresearch.ui.HeadControlPanel;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.Response;

public class ClassInfoActivity extends AppCompatActivity implements
        HeadControlPanel.HeadPanelCallback,View.OnClickListener{
    private HeadControlPanel classInfoHeader;
    private Classes intentClass;
    private TextView classInfoName,classInfoTitle,classInfoTeacher;
    private TextView classInfoTime,classInfoInstruction,classInfoType;
    private TextView classInfoPrice,classInfoDate;
    private Button classInfoPayBtn;
    private Button classInfoDeleteBtn;
    private Button classInfoChangeBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String intentClassStr=getIntent().getStringExtra("intent_class");
        intentClass=Constant.gson.fromJson(intentClassStr,new TypeToken<Classes>(){}.getType());
        setContentView(R.layout.activity_class_info);
        if (getSupportActionBar() != null){
            getSupportActionBar().hide();
        }
        initView();
    }
    private void initView(){
        if(classInfoHeader==null){
            classInfoHeader=(HeadControlPanel)findViewById(R.id.class_info_header);
            classInfoHeader.initHeadPanel();
            classInfoHeader.setHeadPanelCallback(this);
        }
        classInfoHeader.setLeftImage(R.drawable.ic_back);
        classInfoHeader.setMiddleTitle("课程详细信息");
        classInfoHeader.setRightTitle("");
        classInfoName = (TextView) findViewById(R.id.class_info_name);
        classInfoTitle = (TextView) findViewById(R.id.class_info_title);
        classInfoTeacher = (TextView) findViewById(R.id.class_info_teacher);
        classInfoTime = (TextView) findViewById(R.id.class_info_time);
        classInfoInstruction = (TextView) findViewById(R.id.class_info_instruction);
        classInfoType = (TextView) findViewById(R.id.class_info_type);
        classInfoPrice = (TextView) findViewById(R.id.class_info_price);
        classInfoDate = (TextView) findViewById(R.id.class_info_date);

        classInfoPayBtn = (Button) findViewById(R.id.class_info_pay_btn);
        classInfoPayBtn.setOnClickListener(this);
        classInfoDeleteBtn = (Button) findViewById(R.id.class_info_delete_btn);
        classInfoDeleteBtn.setOnClickListener(this);
        classInfoChangeBtn = (Button) findViewById(R.id.class_info_change_btn);
        classInfoChangeBtn.setOnClickListener(this);
        if (intentClass!=null){
            classInfoName.setText(intentClass.getName());
            classInfoTitle.setText(intentClass.getTitle());
            classInfoTeacher.setText(intentClass.getTeacher());
            classInfoTime.setText(intentClass.getTime());
            classInfoInstruction.setText(intentClass.getInstruction());
            classInfoType.setText(intentClass.getType());
            classInfoPrice.setText(""+intentClass.getPrice());
            classInfoDate.setText(intentClass.getDate());
        }
    }
    @Override
    public void onHeadPanelClick(int itemId) {
        switch (itemId){
            case Constant.btn_left_title:
                finish();
                break;
            case Constant.btn_middle_title:
                break;
            case Constant.btn_right_title:
                break;
            default:break;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.class_info_pay_btn:payClasses(intentClass);break;
            case R.id.class_info_delete_btn:deleteClasses(intentClass);break;
            case R.id.class_info_change_btn:changeClasses(intentClass);break;
            default:break;
        }
    }
    Handler handler=new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            if (msg.what==0){
                Toast.makeText(getApplicationContext(),"未知错误",
                        Toast.LENGTH_SHORT).show();
            }else if(msg.what==1){
                String s=(String)msg.obj;
                ResponseInfo responseInfo=Constant.gson.fromJson(s,
                        new TypeToken<ResponseInfo>(){}.getType());
                Toast.makeText(getApplicationContext(),responseInfo.getResponseInfo(),
                        Toast.LENGTH_SHORT).show();
                if(responseInfo.getResponseCode()==200){
                    finish();
                }
            }else if(msg.what==2){
                String s=(String) msg.obj;
                ResponseInfo responseInfo=Constant.gson.fromJson(s,
                        new TypeToken<ResponseInfo>(){}.getType());
                Toast.makeText(getApplicationContext(),responseInfo.getResponseInfo(),
                        Toast.LENGTH_SHORT).show();
            }
            return true;
        }
    });
    private void payClasses(Classes classes){
        LoginAction loginAction=new LoginAction();
        if(!loginAction.hasUserInfo(getApplicationContext())){
            Toast.makeText(getApplicationContext(),"未登录",
                    Toast.LENGTH_SHORT).show();
            return;
        }
        UserInfo userInfo=loginAction.getUserInfo(getApplicationContext());
        ClassesPay pay=new ClassesPay();
        if(userInfo.getType()==Constant.user_student){
            Student student=Constant.gson.fromJson(userInfo.getUser(),
                    new TypeToken<Student>(){}.getType());
            pay=student.payClasses(classes);
        }else if(userInfo.getType()==Constant.user_teacher){
            Teacher teacher=Constant.gson.fromJson(userInfo.getUser(),
                    new TypeToken<Teacher>(){}.getType());
            pay=teacher.payClasses(classes);
        }else if(userInfo.getType()==Constant.user_staff){
            Staffer staffer=Constant.gson.fromJson(userInfo.getUser(),
                    new TypeToken<Staffer>(){}.getType());
            pay=staffer.payClasses(classes);
        }
        final MultipartBody.Builder builder=new MultipartBody.Builder();
        builder.setType(MultipartBody.FORM);
        builder.addFormDataPart("pay_class",Constant.gson.toJson(pay));
        final String url=Constant.URL+"class/pay";
        HttpUtil.sendHttpRequest(url,builder,handler,2,0);
    }
    private void deleteClasses(Classes classes){
        if(!checkPremission(classes))return;
        LoginAction loginAction=new LoginAction();
        UserInfo userInfo=loginAction.getUserInfo(getApplicationContext());
        final MultipartBody.Builder builder=new MultipartBody.Builder();
        builder.setType(MultipartBody.FORM);
        builder.addFormDataPart("delete_class",Constant.gson.toJson(classes));
        final String url=Constant.URL+"class/delete";
        HttpUtil.sendHttpRequest(url,builder,handler,1,0);
    }
    private void changeClasses(Classes classes){
        Intent intent=new Intent(ClassInfoActivity.this,PostClassActivity.class);
        intent.putExtra("change_class",Constant.gson.toJson(classes));
        startActivity(intent);
        finish();
    }
    private boolean checkPremission(Classes classes){
        LoginAction loginAction=new LoginAction();
        if(!loginAction.hasUserInfo(getApplicationContext())){
            Toast.makeText(getApplicationContext(),"未登录",
                    Toast.LENGTH_SHORT).show();
            return false;
        }
        UserInfo userInfo=loginAction.getUserInfo(getApplicationContext());
        if(userInfo.getType()!=Constant.user_teacher){
            Toast.makeText(getApplicationContext(),"不是教师",
                    Toast.LENGTH_SHORT).show();
            return false;
        }
        if (!userInfo.getName().equals(classes.getName())){
            Toast.makeText(getApplicationContext(),"不是本人",
                    Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }
}
